﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 玩具商城管理系统.start
{
    public partial class Changeclient : Form
    {
        public Changeclient()
        {
            InitializeComponent();
        }
        SqlDataAdapter daClient, dalog;
        DataSet ds=new DataSet();
        void showAll()
        {
            DataView dvclient = new DataView(ds.Tables["client_info"]);
            dgvclient.DataSource = dvclient;

        }

        private void Changeclient_Load(object sender, EventArgs e)
        {
           
            init();
            showAll();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvclient_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtid.Text = dgvclient.CurrentRow.Cells["client_id"].Value.ToString();

            txtname.Text = dgvclient.CurrentRow.Cells["client_name"].Value.ToString();
            txtarr.Text = dgvclient.CurrentRow.Cells["client_home"].Value.ToString();
            txtipone.Text = dgvclient.CurrentRow.Cells["client_ipone"].Value.ToString();
            txtemail.Text = dgvclient.CurrentRow.Cells["client_email"].Value.ToString();
            if (dgvclient.CurrentRow.Cells["client_bith"].Value != DBNull.Value && dgvclient.CurrentRow.Cells["client_bith"].Value != null)
            {
                dateTimePicker1.Text = Convert.ToDateTime(dgvclient.CurrentRow.Cells["client_bith"].Value).ToString();
            }
            else
            {
                // 处理 DBNull 或空值的情况，例如设置默认值或清除 dateTimePicker
                dateTimePicker1.Text = ""; // 或者其他合适的默认值
            }
            txtsex.Text = dgvclient.CurrentRow.Cells["client_sex"].Value.ToString();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtname.Text == "" )
            {
                MessageBox.Show("用户名不能为空");
            }
            else
            {
                DialogResult dr=MessageBox.Show("你确定要修改吗？","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    int a=dgvclient.CurrentRow.Index;
                    string str = dgvclient.Rows[a].Cells["client_id"].Value.ToString();
                    DataRow[] CustRow = ds.Tables["client_info"].Select("client_id='" + str + "'");
                    CustRow[0]["client_name"]=txtname.Text;
                    CustRow[0]["client_home"] = txtarr.Text;
                    CustRow[0]["client_ipone"] = txtipone.Text;
                    CustRow[0]["client_email"] = txtemail.Text;
                    CustRow[0]["client_bith"] = Convert.ToDateTime(dateTimePicker1.Text);
                    CustRow[0]["client_sex"] = txtsex.Text;
                    MessageBox.Show("修改成功");

                    DataRow drlog = ds.Tables["log_info"].NewRow();
                    drlog["username"] = login.username;
                    drlog["type"] = "修改";
                    drlog["action_date"] = DateTime.Now;
                    drlog["action_table"] = "client表";
                    ds.Tables["log_info"].Rows.Add(drlog);

                    UpdateDB();
                }
                else
                {
                    return;
                }
            }
        }
        void UpdateDB()
        {
            SqlTransaction st = tansactionInit();
            try
            {
                daClient.Update(ds, "client_info");
                dalog.Update(ds, "log_info");
                st.Commit();
                DB.cn.Close();
            }
            catch(SqlException ex) 
            {
                st.Rollback();
                DB.cn.Close();
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private SqlTransaction tansactionInit()
        {
            DB.GetCn();
            SqlCommandBuilder dbclient=new SqlCommandBuilder(daClient);
            SqlCommandBuilder dblog =new SqlCommandBuilder (dalog);

            daClient.UpdateCommand = dbclient.GetUpdateCommand();
            dalog.InsertCommand = dblog.GetInsertCommand();

            SqlTransaction st=DB.cn.BeginTransaction();
            daClient.UpdateCommand.Transaction = st;
            dalog.InsertCommand.Transaction = st;

            daClient.UpdateCommand.Connection = DB.cn;
            dalog.InsertCommand.Connection= DB.cn;

            return st;
        }
        void init()
        {
            DB.GetCn();
            string str = "select * from client";
            daClient = new SqlDataAdapter(str, DB.cn);
            string sdr = "select * from log";
            dalog =new SqlDataAdapter(sdr, DB.cn);
            daClient.Fill(ds, "client_info");
            dalog.Fill(ds, "log_info");
        }
    }

}
